/**
 * Created by Jacky.Gao on 2017-02-17.
 */
import { resetTableData, undoManager } from "../../Utils.js";
import { MessageBox } from "element-ui";

export function doDeleteCol () {
  const selected = this.getSelected()[0];
  const context = this.context;
  if (!selected) {
    MessageBox.alert(`${window.i18n.table.colTip}`);
    return;
  }
  let startCol = selected[1];
  let endCol = selected[3];
  if (endCol < startCol) {
    const tempStartCol = startCol;
    startCol = endCol;
    endCol = tempStartCol;
  }
  let colWidths = this.getSettings().colWidths;
  let mergeCells = this.getSettings().mergeCells;
  let oldMergeCells = [];
  let newMergeCells = mergeCells.concat([]);
  for (const mergeItem of mergeCells) {
    oldMergeCells.push(Object.assign({}, mergeItem));
    const col = mergeItem.col;
    const colspan = mergeItem.colspan;
    const colEnd = col + colspan - 1;
    const index = newMergeCells.indexOf(mergeItem);
    if (col >= startCol && colEnd <= endCol) {
      newMergeCells.splice(index, 1);
    } else if (col <= startCol && colEnd >= endCol) {
      const span = endCol - startCol + 1;
      let leftSpan = colspan - span;
      if (leftSpan === 0) {
        leftSpan = 1;
      }
      if (leftSpan === 1 && mergeItem.rowspan === 1) {
        newMergeCells.splice(index, 1);
      } else {
        newMergeCells[index] = {
          col: col,
          row: mergeItem.row,
          rowspan: mergeItem.rowspan,
          colspan: leftSpan
        };
      }
    } else if (col > endCol) {
      const totalCols = endCol - startCol + 1;
      newMergeCells[index] = {
        row: mergeItem.row,
        col: col - totalCols,
        rowspan: mergeItem.rowspan,
        colspan: mergeItem.colspan
      };
    }
  }
  this.updateSettings({ mergeCells: [] });
  const dif = endCol - startCol + 1;
  let oldColWidths = colWidths.concat([]);
  let newColWidths = colWidths.concat([]);
  newColWidths.splice(startCol, dif);
  let countRows = this.countRows();
  const removeCells = [];
  for (let i = endCol; i >= startCol; i--) {
    this.alter("remove_col", i);
    for (let j = 0; j < countRows; j++) {
      const cell = context.getCell(j, i);
      if (cell) {
        context.removeCell(cell);
        removeCells.push(cell);
      }
    }
  }
  const cellsMap = context.cellsMap;
  const changeCells = [];
  for (const cell of cellsMap.values()) {
    const colIndex = cell.columnNumber - 1;
    if (colIndex >= endCol) {
      changeCells.push(cell);
    }
  }
  for (const cell of changeCells) {
    context.removeCell(cell);
  }
  for (const cell of changeCells) {
    cell.columnNumber = cell.columnNumber - dif;
    context.addCell(cell);
  }
  this.updateSettings({ colWidths: newColWidths, mergeCells: newMergeCells });
  resetTableData(this);
  context.isModify = true;

  const _this = this;
  undoManager.add({
    redo: function () {
      colWidths = _this.getSettings().colWidths;
      mergeCells = _this.getSettings().mergeCells;
      oldMergeCells = [];
      newMergeCells = mergeCells.concat([]);
      for (const mergeItem of mergeCells) {
        oldMergeCells.push(Object.assign({}, mergeItem));
        const col = mergeItem.col;
        const colspan = mergeItem.colspan;
        const colEnd = col + colspan - 1;
        const index = newMergeCells.indexOf(mergeItem);

        if (col >= startCol && colEnd <= endCol) {
          newMergeCells.splice(index, 1);
        } else if (col <= startCol && colEnd >= endCol) {
          const span = endCol - startCol + 1;
          let leftSpan = colspan - span;
          if (leftSpan === 0) {
            leftSpan = 1;
          }
          if (leftSpan === 1 && mergeItem.rowspan === 1) {
            newMergeCells.splice(index, 1);
          } else {
            newMergeCells[index] = {
              col: col,
              row: mergeItem.row,
              rowspan: mergeItem.rowspan,
              colspan: leftSpan
            };
          }
        } else if (col > endCol) {
          const totalCols = endCol - startCol + 1;
          newMergeCells[index] = {
            row: mergeItem.row,
            col: col - totalCols,
            rowspan: mergeItem.rowspan,
            colspan: mergeItem.colspan
          };
        }
      }
      _this.updateSettings({ mergeCells: [] });
      oldColWidths = colWidths.concat([]);
      newColWidths = colWidths.concat([]);
      newColWidths.splice(startCol, dif);
      countRows = _this.countRows();
      removeCells.splice(0, removeCells.length);
      for (let i = endCol; i >= startCol; i--) {
        for (let j = 0; j < countRows; j++) {
          const cell = context.getCell(j, i);
          if (cell) {
            context.removeCell(cell);
            removeCells.push(cell);
          }
        }
        _this.alter("remove_col", i);
      }
      changeCells.splice(0, changeCells.length);
      for (const cell of cellsMap.values()) {
        const colIndex = cell.columnNumber - 1;
        if (colIndex >= endCol) {
          changeCells.push(cell);
        }
      }
      for (const cell of changeCells) {
        context.removeCell(cell);
      }
      for (const cell of changeCells) {
        cell.columnNumber = cell.columnNumber - dif;
        context.addCell(cell);
      }
      _this.updateSettings({ colWidths: newColWidths, mergeCells: newMergeCells });
      resetTableData(_this);
      context.isModify = true;
    },
    undo: function () {
      for (let i = endCol; i >= startCol; i--) {
        _this.alter("insert_col", i);
      }
      changeCells.splice(0, changeCells.length);
      for (const cell of cellsMap.values()) {
        const colIndex = cell.columnNumber - 1;
        if (colIndex >= startCol) {
          changeCells.push(cell);
        }
      }
      for (const cell of changeCells) {
        context.removeCell(cell);
      }
      for (const cell of changeCells) {
        cell.columnNumber = cell.columnNumber + dif;
        context.addCell(cell);
      }
      for (const cell of removeCells) {
        context.addCell(cell);
      }
      _this.updateSettings({ colWidths: oldColWidths, mergeCells: oldMergeCells });
      resetTableData(_this);
      context.isModify = true;
    }
  });
}
